/*
 * @Author: hongbin
 * @Date: 2022-12-22 09:44:47
 * @LastEditors: hongbin
 * @LastEditTime: 2022-12-22 11:47:54
 * @Description:发布动态
 */
import { NextApiRequest, NextApiResponse } from "next";
import { VerifyFields } from "../../../src/api/utils";
import DB from "../mongodb";
import { receiveRequest } from "../utils";

export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
    if (!receiveRequest(req, res, "POST")) return;
    const db = await DB;
    console.log("发布动态", req.body);

    if (!VerifyFields.has(req.body, ["content", "userId", "userName"])) {
        return res.status(500).json("缺少关键字段");
    }
    const news = { ...req.body, allow: true };
    const insert = await db.collection("dynamic").insertOne(news);
    console.log(insert);
    if (insert.acknowledged) {
        res.status(200).json({ data: { ...news, _id: insert.insertedId } });
        return;
    }
    res.json({ data: null, msg: "插入失败", code: -1 });
}
